<?php
class Arm_Acl_Adapter_File
{
	/**
	 * данные
	 * @var array
	 */
	private $_data = array();
	
	/**
	 * Zend_Acl
	 * 
	 * @var Zend_Acl
	 */
	private $_acl = null;
	
	private $_pathFile = null;
	
    public function  __construct($module, Zend_Acl $acl, $pathFile=null)
    {
       $this->_acl = $acl;
        $this->_pathFile = $pathFile;
       $this->_setData($module);
       return;
    }
    
    /**
     * Установка данных acl
     * @param string $module
     */
    protected function _setData($module)
    {
       if($this->_pathFile === null) {
       $path    = Arm_Core::getPathModule($module) .  'configs/acl.xml';
       } else {
           $path = $this->_pathFile;
       }
       $config  = new Zend_Config_Xml($path);
       $this->_data = $config->toArray();
       return;
    }
    
    /**
     * Фильрация значений 
     * @param mixed $value
     * @return null | array
     */
    private function _filterValue($value)
    {
    if(is_string($value)) {
     		$value = trim($value);
     		if($value === '') {
     			$value = null;
     		}
          return $value; 		
     	}
     	return array_keys($value);
    }
    
    /**
     * Установка ролей.
     */
    public function setRole()
    {
    	if(is_string($this->_data['role']) === true) {
    		return;
    	}
     foreach ($this->_data['role'] as $k=>$v) {
     	$this->_acl->addRole($k, $this->_filterValue($v));
     }	
    }
    
    /**
     * вернет ресурсы
     */
    public function setResource()
    {
    if(is_string($this->_data['resource']) === true) {
    		return;
    	}
    foreach ( $this->_data['resource'] as $k=>$v ) {
    	$this->_acl->addResource($k, $this->_filterValue($v));
    }
    }
    
    public function setAllow()
    {
    if(is_string($this->_data['allow']) === true) {
    		return;
    	}
    foreach ($this->_data['allow'] as $group=>$allow) {
    	if(is_string($allow)) {
    		$this->_acl->allow($group,$this->_filterValue($allow));
    		continue;
    	}
    	foreach ($allow as $k=>$v) {
    	  $this->_acl->allow($group, $k, $this->_filterValue($v));
    	}
    }
    }
    
    /**
     * Установка запретов
     */
    public function setDeny()
    {
    if(is_string($this->_data['deny']) === true) {
    		return;
    	}
    foreach ($this->_data['deny'] as $group=>$allow) {
    	if(is_string($allow)) {
    		$this->_acl->deny($group,$this->_filterValue($allow));
    		continue;
    	}
    	foreach ($allow as $k=>$v) {
    	  $this->_acl->deny($group, $k, $this->_filterValue($v));
    	}
    }
    }
}